<template>
  <div class="app-container">
    <el-row :gutter="20">
      <el-col :span="24" :xs="24">
        <el-form
          :model="queryParams"
          ref="queryForm"
          size="small"
          :inline="true"
          label-width="80px"
        >
          <el-form-item label="请求时间">
            <el-date-picker
              v-model="dateRange"
              style="width: 240px"
              value-format="yyyy-MM-dd HH:mm:ss"
              type="daterange"
              range-separator="-"
              start-placeholder="开始日期"
              end-placeholder="结束日期"
              :default-time="['00:00:00', '23:59:59']"
              size="mini"
              @change="changeTime"
            ></el-date-picker>
          </el-form-item>
          <el-form-item>
            <el-button
              type="primary"
              icon="el-icon-search"
              size="mini"
              @click="handleQuery"
              >搜索</el-button>
            <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
              >重置</el-button>
          </el-form-item>
        </el-form>
      </el-col>
    </el-row>
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          size="mini"
          @click="handleExport"
          v-if="isAdmin"
          >导出</el-button>
      </el-col>
    </el-row>
    <el-table
      v-loading="loading"
      :data="userList"
      :max-height="tableClientHeight"
      border
    >
      <el-table-column
        label="请求时间"
        key="createTime"
        prop="createTime"
      />
      <el-table-column
        label="请求IP"
        key="ip"
        prop="ip"
        width="160"
      />
      <el-table-column
        label="类名"
        key="className"
        prop="className"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        label="方法名"
        key="methodName"
        prop="methodName"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        label="操作"
        key="operation"
        prop="operation"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        label="操作人"
        key="userName"
        prop="userName"
        width="100"
      />
      <el-table-column
        label="操作状态"
        key="status"
        prop="status"
        width="100"
      >
        <template slot-scope="scope">
          {{ scope.row.status === 0 ? "失败" : "成功" }}
        </template>
      </el-table-column>
      <el-table-column
        label="错误信息"
        key="errorMessage"
        prop="errorMessage"
        :show-overflow-tooltip="true"
      />
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNumber"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
  </div>
</template>

<script>
export default {
  name: "Log",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 日期范围
      dateRange: [],
      userList: [],
      total: 0,
      // 查询参数
      queryParams: {
        pageNumber: 1,
        pageSize: 10,
        startTime: undefined,
        endTime: undefined,
      },
    };
  },
  computed: {
    isAdmin() {
      return this.$store.state.user.isAdmin;
    },
    tableClientHeight() {
      let tableClientHeight =
        document.documentElement.clientHeight || document.body.clientHeight;
      tableClientHeight = tableClientHeight - 289;
      return tableClientHeight;
    },
  },
  mounted() {
    this.getList();
  },
  methods: {
    getList() {
      this.loading = true;
      this.$http({
        url: "/log/list",
        method: "GET",
        params: {
          ...this.queryParams,
        },
      }).then(({ data }) => {
        console.log(data);
        this.userList = data.data.data;
        this.total = data.data.totalSize;
        this.loading = false;
      });
    },
    /** 搜索按钮操作 */
    handleQuery() {
        console.log(this.dateRange)
      this.queryParams.pageNumber = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.queryParams = {
        pageNumber: 1,
        pageSize: 10,
        startTime: undefined,
        endTime: undefined,
      }
      this.dateRange = []
      this.handleQuery();
    },
    changeTime(val) {
      this.queryParams.startTime = val ? val[0] : "";
      this.queryParams.endTime = val ? val[1] : "";
    },
    /** 导出按钮操作 */
    handleExport() {
      this.$utils.download({
        url: "/log/export",
        method: "get",
        params: {
          startTime: this.queryParams.startTime,
          endTime: this.queryParams.endTime,
        },
        defaultFileName: `系统日志_${new Date().getTime()}.xlsx`,
      });
    },
  },
};
</script>

<style lang="scss" scoped>
::v-deep .el-pagination {
  right: 10px;
  position: absolute;
  bottom: 10px;
}
.app-container {
  width: 98%;
  background: white;
  border-radius: 10px;
  padding: 20px;
  margin: 1%;
  float: left;
}
</style>
